package com.cxc.distributed.demo;

import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class LockService {

    private static  JedisPool pool = null;
    static {
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(200);
        config.setMaxIdle(8);
        config.setMaxWaitMillis(1000*100);
        config.setTestOnBorrow(true);
        pool = new JedisPool(config, "127.0.0.1", 6379, 3000);
    }
    private LockRedis lockRedis = new LockRedis(pool);

    public void excuteLock() {
        // 1.获取锁
        String idValue = lockRedis.getRedisDistributedLock(5000l, 5000l);
        if (idValue == null) {
            System.out.println(Thread.currentThread().getName() + ",获取锁失败，原因因为获取锁时间超时...");
            return;
        }
        System.out.println(Thread.currentThread().getName() + ",获取锁成功,锁的id:" + idValue + ",正常执行业务了逻辑");

        // 2.释放锁
        lockRedis.unRedisLock(idValue);
    }

}
